package com.txby.platformApi.component.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.R;
import com.txby.pf.modules.sys.controller.AbstractController;
import com.txby.pf.modules.sys.entity.SysDeptEntity;
import com.txby.pf.modules.sys.service.SysDeptService;
import com.txby.platformApi.component.service.SysDeptRegionRelationService;


/**
 * 组织管理
 *
 */
@RestController
@RequestMapping("/sys/customerDept")
public class SysCustomerDeptController extends AbstractController {
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysDeptRegionRelationService sysDeptRegionRelationService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:dept:list")
	public List<SysDeptEntity> list(){
		Map<String, Object> map = new HashMap<>();
		//如果不是超级管理员，则只能查询本组织及子组织数据
		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("deptFilter", sysDeptService.getSubDeptIdList(getDeptId()));
		}
		List<SysDeptEntity> deptList = sysDeptService.queryList(map);

		return deptList;
	}
	@RequestMapping("/listSick/{deptId}")
	public List<SysDeptEntity> listSick(@PathVariable("deptId") Long  deptId){
		Map<String, Object> map = new HashMap<>();
		map.put("deptId", deptId);
		//如果不是超级管理员，则只能查询本组织及子组织数据
//		if(getUserId() != Constant.SUPER_ADMIN){
//			map.put("deptFilter", sysDeptService.getSubDeptIdListSick(getDeptId()));
//		}
		List<SysDeptEntity> deptList = sysDeptService.queryListSick(map);
		
		return deptList;
	}
	@RequestMapping("/listNotSick")
	public List<SysDeptEntity> listNotSick(){
		Map<String, Object> map = new HashMap<>();
		//如果不是超级管理员，则只能查询本组织及子组织数据
		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("deptFilter", sysDeptService.getSubDeptIdList(getDeptId()));
		}
		List<SysDeptEntity> deptList = sysDeptService.queryListNotSick(map);

		return deptList;
		
	}

	/**
	 * 选择组织(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:dept:select")
	public R select(){
		Map<String, Object> map = new HashMap<>();
		//如果不是超级管理员，则只能查询本组织及子组织数据
		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("deptFilter", sysDeptService.getSubDeptIdList(getDeptId()));
		}
		List<SysDeptEntity> deptList = sysDeptService.queryList(map);

		//添加一级组织
		if(getUserId() == Constant.SUPER_ADMIN){
			SysDeptEntity root = new SysDeptEntity();
			root.setDeptId(0L);
			root.setName("一级组织");
			root.setParentId(-1L);
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 上级组织Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("sys:dept:list")
	public R info(){
		long deptId = 0;
		long selfId = 0;
		if(getUserId() != Constant.SUPER_ADMIN){
			SysDeptEntity dept = sysDeptService.queryObject(getDeptId());
			deptId = dept.getParentId();
			selfId=dept.getDeptId();
		}

		return R.ok().put("deptId", deptId).put("selfId", selfId);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("sys:dept:info")
	public R info(@PathVariable("deptId") Long deptId){
		SysDeptEntity dept = sysDeptService.queryObject(deptId);
		
		return R.ok().put("dept", dept);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:dept:save")
	public R save(@RequestBody SysDeptEntity dept){
		
		sysDeptService.save(dept);
//		sysDeptRegionRelationService。save(dept,sysDeptRegionRelation)
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:dept:update")
	public R update(@RequestBody String  json){
////		sysDeptService.update(dept);
//		sysDeptRegionRelationService。update(dept,sysDeptRegionRelation)
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:dept:delete")
	public R delete(long deptId){
		//判断是否有子组织
		List<Long> deptList = sysDeptService.queryDetpIdList(deptId);
		if(deptList.size() > 0){
			return R.error("请先删除子组织");
		}

		sysDeptService.delete(deptId);
		
		return R.ok();
	}
	
}
